<script setup>
import { ref, reactive } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import request from '../../api/request'

const router = useRouter()
const formRef = ref()

const registerForm = reactive({
  username: '',
  password: '',
  realName: ''
})

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  realName: [{ required: true, message: '请输入真实姓名', trigger: 'blur' }]
}

const handleRegister = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    const res = await request({
      url: '/employee/register',
      method: 'post',
      data: registerForm
    })
    ElMessage.success('注册成功')
    router.push('/login')
  } catch (error) {
    ElMessage.error(error.message || '注册失败，请重试')
  }
}
</script>

<template>
  <div class="register-container">
    <img src="../../assets/pic/EmployeeRegister.png" alt="左上角装饰" class="left-decoration" />
    <img src="../../assets/pic/Right.png" alt="右上角装饰" class="right-decoration" />
    <div class="register-right">
      <div class="register-form-container">
        <h1 class="register-title">员工注册</h1>
        <p class="slogan">在议启，让会议高效开启</p>
        <el-form
          ref="formRef"
          :model="registerForm"
          :rules="rules"
          class="register-form">
          
          <!-- 用户名输入框 -->
          <el-form-item prop="username">
            <el-input
              v-model="registerForm.username"
              placeholder="用户名"
              :prefix-icon="User"
            />
          </el-form-item>
          
          <!-- 密码输入框 -->
          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              type="password"
              placeholder="密码"
              :prefix-icon="Lock"
            />
          </el-form-item>

          <!-- 真实姓名输入框 -->
          <el-form-item prop="realName">
            <el-input
              v-model="registerForm.realName"
              placeholder="真实姓名"
              :prefix-icon="User"
            />
          </el-form-item>
          
          <!-- 注册按钮 -->
          <el-form-item>
            <el-button
              type="primary"
              class="register-button"
              @click="handleRegister"
            >
              注册
            </el-button>
          </el-form-item>
          <div class="login-section">
            <p>
              已有账号？点击"<a class="login-link" @click="router.push(`/login`)">登录</a>"使用会议室预订系统
            </p>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style scoped>
@font-face {
  font-family: 'HYYaKuHei';
  src: url('../../assets/fonts/汉仪雅酷黑-85.ttf') format('truetype');
}

.login-section {
  margin-top: 1.5rem;
  text-align: center;
  color: rgba(19, 19, 19, 0.8);
  font-size: 0.9rem;
}

.login-section p {
  line-height: 1.5;
}

.login-link {
  color: #c23400;
  font-weight: 600;
  cursor: pointer;  /* 添加鼠标指针样式 */
  text-decoration: none;
  transition: all 0.3s ease;
}

.register-container {
  display: flex;
  /* min-height: 520px; */
  /* height: 80vh; */
  width: 80%;
  max-width: 600px;
  margin: 0 auto; 
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  border-radius: 30px;
  background: linear-gradient(to bottom, #c23400c7 0%, #ffffff 20%);
  position: relative;
  padding: 40px;
  position: absolute; /* 添加绝对定位 */
  top: 50%; /* 从顶部50%开始 */
  left: 50%; /* 从左侧50%开始 */
  transform: translate(-50%, -50%); /* 使用transform进行居中偏移 */
}

.left-decoration {
  position: absolute;
  top: 40px;
  left: 50px;
  width: 240px;
  height: auto;
  opacity: 0.8;
  z-index: 1;
}

.right-decoration {
  position: absolute;
  top: 40px;
  right: 40px;
  width: 240px;
  height: auto;
  opacity: 0.8;
  z-index: 1;
}

.register-right {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: flex-start;
  padding: 120px 2rem 0rem;
  position: relative;
  z-index: 2;
}

.register-form-container {
  width: 100%;
  text-align: left;
  /* font-family: 'HYYaKuHei', sans-serif; */
}

.register-title {
  text-align: left;
  font-size: 1.8rem;
  margin-bottom: 6px;
  font-family: 'HYYaKuHei', sans-serif;
  color: #c23400;
}

.slogan {
  text-align: left;
  font-size: 14px;
  color: #666;
  margin-bottom: 12px;
}

.register-form {
  margin-top: 20px;
}

.register-button {
  height: 46px;
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
  border-radius: 8px;
  background: #c23400;
  border: none;
}

.register-button:hover {
  background: #c23400;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  padding: 8px 12px;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #c23400 inset;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

/* 添加以下样式来修改表单高亮颜色 */
:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #c23400 inset !important;
}

:deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: #c23400 !important;
  border-color: #c23400 !important;
}

:deep(.el-radio__input.is-checked .el-radio__inner) {
  background-color: #c23400 !important;
  border-color: #c23400 !important;
}

:deep(.el-switch.is-checked .el-switch__core) {
  border-color: #c23400 !important;
  background-color: #c23400 !important;
}

:deep(.el-slider__bar) {
  background-color: #c23400 !important;
}

:deep(.el-slider__button) {
  border-color: #c23400 !important;
}
</style>